home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zfilter.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  220 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfilter.c */
  20. /* Filter creation for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "alloc.h"
  25. #include "stream.h"
  26.  
  27. /* Forward references */
  28. int filter_read(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  29. int filter_write(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  30.  
  31. /* Imported from zfile.c */
  32. extern const uint file_default_buffer_size;
  33.  
  34. /* <source> .filter_ASCIIHexEncode <file> */
  35. extern const stream_procs s_AXE_procs;
  36. int
  37. zAXE(os_ptr op)
  38. {    return filter_write(op, &s_AXE_procs, NULL, 0);
  39. }
  40.  
  41. /* <target> .filter_ASCIIHexDecode <file> */
  42. extern const stream_procs s_AXD_procs;
  43. extern void s_AXD_init(P1(stream *));
  44. int
  45. zAXD(os_ptr op)
  46. {    stream *s;
  47.     int code = filter_read(op, &s_AXD_procs, &s, 0);
  48.     if ( code < 0 ) return code;
  49.     s_AXD_init(s);
  50.     return code;
  51. }
  52.  
  53. /* <source> <seed> .filter_eexecDecode <file> */
  54. extern const stream_procs s_exD_procs;
  55. extern void s_exD_init(P2(stream *, ushort));
  56. int
  57. zexD(register os_ptr op)
  58. {    stream *s;
  59.     ushort state;
  60.     int code;
  61.     check_type(*op, t_integer);
  62.     state = op->value.intval;
  63.     if ( op->value.intval != state )
  64.         return_error(e_rangecheck);    /* state value was truncated */
  65.     /* The buffer size of 512 is part of the specification of eexec. */
  66.     code = filter_read(op - 1, &s_exD_procs, &s, 512);
  67.     if ( code < 0 ) return code;
  68.     s_exD_init(s, state);
  69.     pop(1);
  70.     return 0;
  71. }
  72.  
  73. /* <target> .filter_NullEncode <file> */
  74. extern const stream_procs s_NullE_procs;
  75. int
  76. zNullE(os_ptr op)
  77. {    stream *s;
  78.     int code = filter_write(op, &s_NullE_procs, &s, 0);
  79.     if ( code >= 0 )
  80.     {    /* NullEncode filters can report their position */
  81.         s->modes |= s_mode_seek;
  82.     }
  83.     return code;
  84. }
  85.  
  86. /* <source> <bool> .filter_PFBDecode <file> */
  87. extern const stream_procs s_PFBD_procs;
  88. extern void s_PFBD_init(P2(stream *, int));
  89. int
  90. zPFBD(os_ptr op)
  91. {    stream *s;
  92.     int code;
  93.     check_type(*op, t_boolean);
  94.     code = filter_read(op - 1, &s_PFBD_procs, &s, 0);
  95.     if ( code < 0 ) return code;
  96.     s_PFBD_init(s, op->value.index);
  97.     pop(1);
  98.     return 0;
  99. }
  100.  
  101. /* <source> <EODcount> <EODstring> .filter_SubFileDecode <file> */
  102. extern const stream_procs s_SFD_procs;
  103. extern void s_SFD_init(P4(stream *, long, const byte *, uint));
  104. int
  105. zSFD(os_ptr op)
  106. {    stream *s;
  107.     int code;
  108.     check_type(op[-1], t_integer);
  109.     check_read_type(*op, t_string);
  110.     if ( op[-1].value.intval < 0 )
  111.         return_error(e_rangecheck);
  112.     code = filter_read(op - 2, &s_SFD_procs, &s, r_size(op) * 2);
  113.     if ( code < 0 ) return code;
  114.     s_SFD_init(s, op[-1].value.intval, op->value.const_bytes, (uint)r_size(op));
  115.     pop(2);
  116.     return 0;
  117. }
  118.  
  119. /* ------ Utilities ------ */
  120.  
  121. /* Free the underlying string stream if needed. */
  122. private void
  123. filter_free_null(stream *s)
  124. {
  125. }
  126. private void
  127. filter_free_stream(stream *s)
  128. {    alloc_free((char *)s->strm, 1, sizeof(stream),
  129.            "filter_free_stream(stream)");
  130. }
  131.  
  132. /* Set up an input filter. */
  133. int
  134. filter_read(os_ptr op, const stream_procs _ds *procs, stream **ps,
  135.   uint min_size)
  136. {    stream *s;
  137.     stream *sstrm;
  138.     int is_temp;
  139.     int code;
  140.     /* Check to make sure that the underlying data */
  141.     /* can function as a source for reading. */
  142.     switch ( r_type(op) )
  143.        {
  144.     case t_string:
  145.         check_read(*op);
  146.         sstrm = (stream *)alloc(1, sizeof(stream),
  147.                     "filter_read(string stream)");
  148.         if ( sstrm == 0 )
  149.             return_error(e_VMerror);
  150.         sread_string(sstrm, op->value.bytes, r_size(op));
  151.         is_temp = 1;
  152.         break;
  153.     case t_file:
  154.         check_read(*op);
  155.         sstrm = op->value.pfile;
  156.         is_temp = 0;
  157.         break;
  158.     default:
  159.         return_error(e_typecheck);
  160.        }
  161.     code = filter_open("r", max(min_size, file_default_buffer_size),
  162.                (ref *)op, procs, &s);
  163.     if ( code < 0 ) return code;
  164.     s->strm = sstrm;
  165.     s->strm_is_temp = is_temp;
  166.     if ( ps != NULL ) *ps = s;
  167.     return 0;
  168. }
  169.  
  170. /* Set up an output filter. */
  171. int
  172. filter_write(os_ptr op, const stream_procs _ds *procs, stream **ps,
  173.   uint min_size)
  174. {    stream *s;
  175.     stream *sstrm;
  176.     int is_temp;
  177.     int code;
  178.     /* Check to make sure that the underlying data */
  179.     /* can function as a sink for writing. */
  180.     switch ( r_type(op) )
  181.        {
  182.     case t_string:
  183.         check_write(*op);
  184.         sstrm = (stream *)alloc(1, sizeof(stream),
  185.                     "filter_write(string)");
  186.         if ( sstrm == 0 )
  187.             return_error(e_VMerror);
  188.         swrite_string(sstrm, op->value.bytes, r_size(op));
  189.         is_temp = 1;
  190.         break;
  191.     case t_file:
  192.         check_write(*op);
  193.         sstrm = op->value.pfile;
  194.         is_temp = 0;
  195.         break;
  196.     default:
  197.         return_error(e_typecheck);
  198.        }
  199.     code = filter_open("w", max(min_size, file_default_buffer_size),
  200.                (ref *)op, procs, &s);
  201.     if ( code < 0 )
  202.         return code;
  203.     s->strm = sstrm;
  204.     s->strm_is_temp = is_temp;
  205.     if ( ps != NULL ) *ps = s;
  206.     return 0;
  207. }
  208.  
  209. /* ------ Initialization procedure ------ */
  210.  
  211. op_def zfilter_op_defs[] = {
  212.     {"1.filter_ASCIIHexEncode", zAXE},
  213.     {"1.filter_ASCIIHexDecode", zAXD},
  214.     {"2.filter_eexecDecode", zexD},
  215.     {"1.filter_NullEncode", zNullE},
  216.     {"2.filter_PFBDecode", zPFBD},
  217.     {"1.filter_SubFileDecode", zSFD},
  218.     op_def_end(0)
  219. };
  220.